package edu.scau.yyl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import edu.scau.yyl.dao.UserDAO;
import edu.scau.yyl.vo.User;
import esu.scau.yyl.dbc.DataBaseConnection;

public class userDAOImpl implements UserDAO {
	
	private DataBaseConnection dbc = null;
	private Connection con = null;
	/*public userDAOImpl() throws Exception{
		this.dbc = new DataBaseConnection();
		this.con = this.dbc.getConnection();
	}*/

	@Override
	public boolean save(User user) throws Exception {
		this.dbc = new DataBaseConnection();
		this.con = this.dbc.getConnection();
		boolean flag = false;
		PreparedStatement pstmt = null;
		String sql = "INSERT INTO user(userName,password) VALUES (?,?) ";
		try {
			pstmt = this.con.prepareStatement(sql);
			//pstmt.setInt(1, user.getUserID()); // 所有的内容从user类中取出
			pstmt.setString(1, user.getUserName()); // 所有的内容从user类中取出
			pstmt.setString(2, user.getPassword()); // 所有的内容从user类中取出
			if (pstmt.executeUpdate() > 0) {// 至少已经更新了一行
				flag = true;
			}
		} catch (Exception e) {
			throw e;
		} finally { // 进行数据库的关闭操作
			if (pstmt != null) {
				try {
					pstmt.close();
				} catch (Exception e1) {

				}
			}
			this.dbc.close();
		}
		return flag;
	}

	@Override
	public boolean delete(User user) throws Exception {
		this.dbc = new DataBaseConnection();
		this.con = this.dbc.getConnection();
		boolean flag = false;
		PreparedStatement pstmt = null;
		String sql = "DELETE FROM user WHERE userID=? ";
		try {
			pstmt = this.con.prepareStatement(sql);
			pstmt.setInt(1, user.getUserID()); // 所有的内容从user类中取出
			if (pstmt.executeUpdate() > 0) {// 至少已经更新了一行
				flag = true;
			}
		} catch (Exception e) {
			throw e;
		} finally { 
			if (pstmt != null) {
				try {
					pstmt.close();
				} catch (Exception e1) {

				}
			}
			this.dbc.close();
		}
		return flag;
	
		
	}

	@Override
	public User findById(int userID) throws Exception {
		this.dbc = new DataBaseConnection();
		this.con = this.dbc.getConnection();
		PreparedStatement pstmt = null;
		User user = null;
		String sql = "SELECT userID,userName,password FROM user WHERE userID=?";
		try {
			
		pstmt = con.prepareStatement(sql);
		pstmt.setInt(1,userID);
		ResultSet rs = pstmt.executeQuery();
		
		if(rs.next()){
			String userName = rs.getString(2);
			String password = rs.getString(3);
			user = new User();
			user.setUserID(userID);
			user.setUserName(userName);
			user.setPassword(password);
		}
		rs.close();	
		}catch (Exception e) {
			throw e;
		} finally { // 不管如何抛出，最终肯定是要进行数据库的关闭操作的
			if (pstmt != null) {
				try {
					pstmt.close();
				} catch (Exception e1) {

				}
			}
			this.dbc.close();
		}
		return user;
	}
	

	@Override
	public List<User> findAll() throws Exception {
		this.dbc = new DataBaseConnection();
		this.con = this.dbc.getConnection();
		PreparedStatement pstmt = null;
		User user = null;
		List<User> list = new ArrayList<User>();
		String sql = "SELECT userID,userName,password FROM user";
		try {
		pstmt = con.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		
		while(rs.next()){
			int userID = rs.getInt(1);
			String userName = rs.getString(2);
			String password = rs.getString(3);
			user = new User();
			user.setUserID(userID);
			user.setUserName(userName);
			user.setPassword(password);
			list.add(user);
		}
		rs.close();
	}catch (Exception e) {
		throw e;
	} finally { // 不管如何抛出，最终肯定是要进行数据库的关闭操作的
		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (Exception e1) {

			}
		}
		this.dbc.close();
	}
	return list;
}

	@Override
	public boolean update(User user) throws Exception {
		this.dbc = new DataBaseConnection();
		this.con = this.dbc.getConnection();
		boolean flag = false;
		PreparedStatement pstmt = null;
		String sql = "UPDATE user SET userName=?,password=? WHERE userID=?";
		try {
			pstmt = this.con.prepareStatement(sql);
			pstmt.setString(1, user.getUserName()); // 所有的内容从user类中取出
			pstmt.setString(2, user.getPassword()); // 所有的内容从user类中取出
			pstmt.setInt(3, user.getUserID()); 
			if (pstmt.executeUpdate() > 0) {// 至少已经更新了一行
				flag = true;
			}
		} catch (Exception e) {
			throw e;
		} finally { 
			if (pstmt != null) {
				try {
					pstmt.close();
				} catch (Exception e1) {

				}
			}
			this.dbc.close();
		}
		return flag;
	}

	@Override
	public User findByUserName(String userName) throws Exception {
		this.dbc = new DataBaseConnection();
		this.con = this.dbc.getConnection();
		PreparedStatement pstmt = null;
		User user = null;
		String sql = "SELECT userID,userName,password FROM user WHERE userName=?";
		try {
			
		pstmt = con.prepareStatement(sql);
		pstmt.setString(1,userName);
		ResultSet rs = pstmt.executeQuery();
		
		if(rs.next()){
			int userID = rs.getInt(1);
			String password = rs.getString(3);
			user = new User();
			user.setUserID(userID);
			user.setUserName(userName);
			user.setPassword(password);
		}
		rs.close();	
		}catch (Exception e) {
			throw e;
		} finally { 
			if (pstmt != null) {
				try {
					pstmt.close();
				} catch (Exception e1) {

				}
			}
			this.dbc.close();
		}
		return user;
	}

	
	
	

}
